package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/http/requests"
	"goravel/app/models"
)

type CarController struct {
	//Dependent services
}

func NewCarController() *CarController {
	return &CarController{
		//Inject services
	}
}

// Index 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         CarController
// @Accept       json
// @Produce      json
// @Id CarIndex
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param  name  query  string  false  "name"
// @Param  pageSize  query  string  false  "pageSize"
// @Param  currentPage  query  string  false  "currentPage"
// @Param  sort  query  string  false  "sort"
// @Param  order  query  string  false  "order"
// @Success 200 {string} json {}
// @Router       /api/admin/car [get]
func (r *CarController) Index(ctx http.Context) http.Response {
	cars := []models.Car{}
	queries := ctx.Request().Queries()
	res, err := httpfacade.NewResult(ctx).SearchByParams(queries, nil).ResultPagination(&cars, "Kind")
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "", err.Error())
	}
	return res
}

// List 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         CarController
// @Accept       json
// @Produce      json
// @Id CarList
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param  mobile  query  string  false  "mobile"
// @Param  pageSize  query  string  false  "pageSize"
// @Param  currentPage  query  string  false  "currentPage"
// @Param  sort  query  string  false  "sort"
// @Param  order  query  string  false  "order"
// @Success 200 {string} json {}
// @Router       /api/admin/car/list [get]
func (r *CarController) List(ctx http.Context) http.Response {
	cars := []models.Car{}
	queries := ctx.Request().Queries()
	return httpfacade.NewResult(ctx).SearchByParams(queries, nil).Success("", cars)
}

func (r *CarController) Show(ctx http.Context) http.Response {
	return nil
}

// Store 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         CarController
// @Accept       json
// @Produce      json
// @Id CarStore
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param carData body requests.CarRequest true "用户数据"
// @Success 200 {string} json {}
// @Router       /api/admin/car [post]
func (r *CarController) Store(ctx http.Context) http.Response {
	var carRequest requests.CarRequest
	errors, err := ctx.Request().ValidateRequest(&carRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}

	facades.Orm().Query().Model(&models.Car{}).Create(&models.Car{
		Serial:      carRequest.Serial,
		KindID:      carRequest.KindID,
		Inspection:  carRequest.Inspection,
		Maintenance: carRequest.Maintenance,
		State:       carRequest.State,
	})
	return httpfacade.NewResult(ctx).Success("创建成功", nil)
}

// Update 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         CarController
// @Accept       json
// @Produce      json
// @Id CarUpdate
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param carData body requests.CarRequest true "用户数据"
// @Success 200 {string} json {}
// @Router       /api/admin/car/{id} [put]
func (r *CarController) Update(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	car := models.Car{}
	facades.Orm().Query().Model(&models.Car{}).Where("id=?", id).Find(&car)
	var carRequest requests.CarRequest
	errors, err := ctx.Request().ValidateRequest(&carRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	facades.Orm().Query().Model(&models.Car{}).Where("id=?", id).Update(&models.Car{
		Serial:      carRequest.Serial,
		KindID:      carRequest.KindID,
		Inspection:  carRequest.Inspection,
		Maintenance: carRequest.Maintenance,
		State:       carRequest.State,
	})
	return httpfacade.NewResult(ctx).Success("修改成功", nil)
}

// Destroy 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         CarController
// @Accept       json
// @Produce      json
// @Id CarDestroy
// @Security ApiKeyAuth
// @Param carData body requests.CarRequest true "用户数据"
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/car/{id} [delete]
func (r *CarController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	car := models.Car{}
	facades.Orm().Query().Model(&models.Car{}).Where("id=?", id).Delete(&car, id)
	return httpfacade.NewResult(ctx).Success("删除成功", nil)
}

func (r *CarController) MiniList(ctx http.Context) http.Response {
	serial := ctx.Request().Query("serial")

	cars := []models.Car{}
	queries := ctx.Request().Queries()
	if serial == "" {
		list, _ := httpfacade.NewResult(ctx).SearchByParams(queries, nil, []string{"serial"}...).List(&cars)
		return list
	} else {
		list, _ := httpfacade.NewResult(ctx).SearchByParams(queries, nil).List(&cars)
		return list
	}
}
